<?php

namespace app\admin\controller;

use app\admin\model\Classes;
use app\admin\model\Course;
use app\admin\model\Student;
use app\admin\model\Grade;
use app\admin\validate\StudentValidate;
use app\admin\validate\StudentregValidate;
use app\admin\model\Campus;
use Think\Upload;
use think\File;
use think\Db;

/**
 * 职位信息 Controller
 * peter
 * Class Student
 * @package app\admin\controller
 */
class JobController extends CommonController {

    /**
     * 获得学生信息json数据
     */
    public function getJobs() {
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $path = $this->getDataByCampusid($_POST);
        $jobs = Db::table("ew_job")->alias('j')
            ->join('ew_u_user u','j.job_user_id=u.user_id','LEFT')
            ->join('ew_area a','j.job_shi=a.district_id','LEFT')
            ->where($path)
            ->field('job_id,user_id,job_name,job_type,job_status,job_createtime,user_username,user_phone,district')
            ->order('job_createtime DESC')
            ->limit($rows * ($page - 1), $rows)
            ->select();
        $total = Db::table("ew_job")->alias('j')
            ->join('ew_u_user u','j.job_user_id=u.user_id','LEFT')
            ->join('ew_area a','j.job_shi=a.district_id','LEFT')
            ->where($path)
            ->field('job_id')
            ->count();
        $data['total'] = $total;
        $data['rows'] = $jobs;
        return json_encode($data);
    }


    /**
     * 删除职位
     */
    public function deleteByID() {
        $jid = $_POST['jid'];
        $deleteinfo = Db::table('ew_job')->where('job_id',$jid)->delete();
        if ($deleteinfo) {
            return json_encode(array("status" => 1, "msg" => "删除成功！"));
        } else {
            return json_encode(array("status" => 0, "msg" => "删除失败！"));
        }
    }
    /**
     * 禁用
     */
    public function jinyong() {
        $jid = $_POST['jid'];
        $deleteinfo = Db::table('ew_job')->where('job_id',$jid)->update(['job_status'=>2]);
        if ($deleteinfo) {
            return json_encode(array("status" => 1, "msg" => "成功！"));
        } else {
            return json_encode(array("status" => 0, "msg" => "失败！"));
        }
    }
    /**
     * 禁用
     */
    public function qiyong() {
        $jid = $_POST['jid'];
        $deleteinfo = Db::table('ew_job')->where('job_id',$jid)->update(['job_status'=>1]);
        if ($deleteinfo) {
            return json_encode(array("status" => 1, "msg" => "成功！"));
        } else {
            return json_encode(array("status" => 0, "msg" => "失败！"));
        }
    }
     /**
     * 查看职位详情
     */
    public function jobInfo() {
        $jid = $_POST['jid'];
        $field = 'j.*,user_id,u.district sheng,ua.district shi,uaa.district qu';
        $field .= ',user_username,user_image';
        $data = Db::table('ew_job')->alias('j')
            ->join('ew_u_user uu','j.job_user_id=uu.user_id','LEFT')
            ->join('ew_area u','j.job_sheng=u.district_id','LEFT')
            ->join('ew_area ua','j.job_shi=ua.district_id','LEFT')
            ->join('ew_area uaa','j.job_qu=uaa.district_id','LEFT')
            ->where('job_id',$jid)->field($field)
            ->find();
        if($data['job_type'] == 2){//如果是公司的话有公司地址
            $data['com_sheng'] = Db::table('ew_area')->where('district_id',$data['job_com_sheng'])->find()['district'];
            $data['com_shi'] = Db::table('ew_area')->where('district_id',$data['job_com_shi'])->find()['district'];
            $data['com_qu'] = Db::table('ew_area')->where('district_id',$data['job_com_qu'])->find()['district'];
        }
        $sex = array('不限','男','女');
        $school = array('无','小学','初中','高中','本科','硕士','博士','大专');
        $year = array('无','应届生','1年以内','1-3年','3-5年','5-10年','10年以上','不限');
        $data['job_sex'] = $sex[$data['job_sex']];
        $data['job_school'] = $school[$data['job_school']];
        $data['job_year'] = $year[$data['job_year']];
//        图片
        $img = array();
        if($data['job_beautiful']){
            $img = explode(',',$data['job_beautiful']);
        }
        $this->assign("data", $data);
        $this->assign("img", $img);
        return $this->fetch("job/job_info");
    }






    /**
     * Author
     * Blog www..com
     * 导出为excel
     */
    public function export() {
        $campusid = session('loginSession')['campusid'];
        $campus = Campus::get($campusid);
        $xlsName = $campus->campus_name . "校区学员信息汇总表";
        $xlsCell = array(
            array('student_name', '姓名'),
            array('student_studentid', '学号'),
            array('student_phone', '联系方式'),
            array('student_idcard', '身份证号'),
            array('student_school', '就读学校'),
            array('grade_name', '年级'),
            array('student_sex', '性别'),
            array('course_name', '选择课程'),
            array('student_cardnum', '支付宝账号'),
            array('student_createtime', '报名日期'),
            array('classes_name','班级'),
            array('student_status', '状态'),
            array('student_remark', '备注')
        );
        $join = [
            ['ew_grade grade', 'job.student_gradeid=grade.id'],
            ['ew_course course', 'job.student_courseid=course.id'],
            ['ew_classes classes','job.student_classid=classes.id'],
        ];
        $xlsData = Db::table('ew_student')->alias('job')->join($join)->order('student_studentid asc')->select();
        for ($i = 0; $i < count($xlsData); $i++) {
            if ($xlsData[$i]['student_status'] == 0) {
                $xlsData[$i]['student_status'] = "在读";
            } else {
                $xlsData[$i]['student_status'] = "毕业";
            }
            if ($xlsData[$i]['student_sex'] == 0) {
                $xlsData[$i]['student_sex'] = "男";
            } else {
                $xlsData[$i]['student_sex'] = "女";
            }
        }
        $this->exportExcel($xlsName, $xlsCell, $xlsData);
    }

    /**
     * 导入一定格式的数据
     */
    public function import() {
        $files = request()->file('Filedata');
        $file = $files->move('static/plugins/AjaxUpload/Upload');
        $fil = $file->getPathname();
        $array = $this->read($fil);
        
        $arr = array();
      
        $campusid = session('loginSession')['campusid'];
     
        for($i=2;$i<count($array)+1;$i++){
            foreach($array[$i] as $k=>$v){
                if($k==0){
                    $array[$i]['student_name'] = $v;
                    unset($array[$i][$k]);
                }
              
                if($k==1){
                    $array[$i]['student_phone'] = $v;
                    unset($array[$i][$k]);
                }
                if($k==2){
                    $array[$i]['student_idcard'] = $v;
                    unset($array[$i][$k]);
                }
                if($k==3){
                    $array[$i]['student_school'] = $v;
                    unset($array[$i][$k]);
                }
                if($k==4){
                    $array[$i]['student_gradeid'] = $v;
                    unset($array[$i][$k]);
                }
                if($k==5){
                    $array[$i]['student_sex'] = $v;
                    unset($array[$i][$k]);
                }
                if($k==6){
                    $array[$i]['student_courseid'] = $v;
                    unset($array[$i][$k]);
                }
                if($k==7){
                    $array[$i]['student_cardnum'] = $v;
                    unset($array[$i][$k]);
                }
                
                if($k==8){
                    $array[$i]['student_classid'] = $v;
                    unset($array[$i][$k]);
                }
                if($k==9){
                    $array[$i]['student_status'] = $v;
                    unset($array[$i][$k]);
                }
                if($k==10){
                    $array[$i]['student_remark'] = $v;
                    unset($array[$i][$k]);
                }
                $array[$i]['campusid'] = $campusid;
                $array[$i]['student_createtime'] = date("Y-m-d",time());
                $array[$i]['student_studentid'] = "";
            }
           $arr[$i-2]=$array[$i];
           
        }
      

        for($i=0;$i<count($arr);$i++){
                if( null != $a = Db::table("ew_grade")->field("id")->where(["grade_name"=>$arr[$i]["student_gradeid"],"campusid"=>$campusid])->select()){
                    $arr[$i]["student_gradeid"]=$a[0]['id'];
                }else{
                    $id = Db::table("ew_grade")->insertGetId(["campusid"=>$campusid,"grade_name"=>$arr[$i]["student_gradeid"]]);
                    $arr[$i]["student_gradeid"]=$id;
                }
          
                if(null != $a = Db::table("ew_course")->field("id")->where(["course_name"=>$arr[$i]["student_courseid"],"campusid"=>$campusid])->select()){
                    $arr[$i]["student_courseid"]=$a[0]['id'];
                }else{
                    $id = Db::table("ew_course")->insertGetId(["campusid"=>$campusid,"course_name"=>$arr[$i]["student_courseid"]]);
                    $arr[$i]["student_courseid"] = $id;
                }
                if(null != $a = Db::table("ew_classes")->field("id")->where(["classes_name"=>$arr[$i]["student_classid"],"campusid"=>$campusid])->select()){
                    $arr[$i]["student_classid"]=$a[0]['id'];
                }else{
                    $id = Db::table("ew_classes")->insertGetId(["campusid"=>$campusid,"classes_name"=>$arr[$i]["student_classid"]]);
                    $arr[$i]["student_classid"]=$id;
                }
                if($arr[$i]['student_sex']=='女'){
                    $arr[$i]['student_sex']=0;
                }else{
                    $arr[$i]['student_sex']=1;
                }
                if($arr[$i]['student_status']=='在读'){
                    $arr[$i]['student_status']=0;
                }else{
                    $arr[$i]['student_status']=1;
                }
                
         
        }

        for($k=0;$k<count($arr);$k++){
            $count = Db::table('ew_student')->where('student_gradeid', $arr[$k]['student_gradeid'])->where('student_classid', $arr[$k]['student_classid'])->count();
             if($arr[$k]["student_studentid"]==''){
                    $arr[$k]["student_studentid"]=date('Y') . str_pad($arr[$k]['student_gradeid'], 2, 0, STR_PAD_LEFT) . str_pad($arr[$k]['student_classid'], 2, 0, STR_PAD_LEFT) . str_pad( ++$count, 2, 0, STR_PAD_LEFT);;
                }
            $id = M("job")->insertGetId($arr[$k]);
            $b = sprintf("%06d", $id);
            M("user")->insert(["campusid" => $campusid, "username" => $b, "password" => $b, "typeid" => 1]);
        }
      
       
        
    }
  
} 